<?php

namespace App\Repositories\System;

use App\Models\System\User;
use App\Presenters\System\UserPresenter;
use App\Repositories\BaseRepository;
use App\Validators\System\UserValidator;
use Prettus\Repository\Criteria\RequestCriteria;

/**
 * Class UserRepositoryEloquent
 * @package namespace App\Repositories\System;
 */
class UserRepositoryEloquent extends BaseRepository implements UserRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return User::class;
    }

    /**
    * Specify Validator class name
    *
    * @return mixed
    */
    public function validator()
    {

        return UserValidator::class;
    }


    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    public function presenter()
    {
        return UserPresenter::class;
    }

    public function refreshPermission($user_id){
        $user=$this->skipPresenter()->find($user_id);
        $stations=$user->stations;
        $roles_ids=[];
        if($stations){
            foreach ($stations as $key=>$station)
            array_push($roles_ids,$station->role_id);
        }
        $user->detachRoles();
        $user->attachRole(['id'=>$roles_ids]);
    }
}
